//
//  SaveTool.m
//  UI19_豆瓣练习
//
//  Created by dllo on 15/10/13.
//  Copyright (c) 2015年 lanou3g. All rights reserved.
//
#if 0
#import "SaveTool.h"
#import "ZBCartoonHistoryModel.h"
#import "ZBHomeCartoonModel.h"

@implementation SaveTool


+ (void)isHaveFolder:(SaveToolType)type {
    switch (type) {
        case SaveToolCartoonBookCase: {
            NSString *sandBoxPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
            NSString *movieFile = [sandBoxPath stringByAppendingPathComponent:@"BookCase"];
            NSFileManager *manager = [NSFileManager defaultManager];
            if (![manager fileExistsAtPath:movieFile]) {
                [manager createDirectoryAtPath:movieFile withIntermediateDirectories:YES attributes:nil error:nil];
            }
            break;
        }
        case SaveToolCartoonHistory: {
            NSString *sandBoxPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
            NSString *movieFile = [sandBoxPath stringByAppendingPathComponent:@"CartoonHistory"];
            NSFileManager *manager = [NSFileManager defaultManager];
            if (![manager fileExistsAtPath:movieFile]) {
                [manager createDirectoryAtPath:movieFile withIntermediateDirectories:YES attributes:nil error:nil];
            }
            break;
        }
        default:
            break;
    }
    
}

//+ (BOOL)isHaveMovieInPlist:(Movie *)movie {
//    
//    NSString *sandBoxPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
//    NSString *docPath = [sandBoxPath stringByAppendingPathComponent:@"movie/movie.plist"];
//    NSArray *movieArr = [NSKeyedUnarchiver unarchiveObjectWithFile:docPath];
//    NSLog(@"%@", movieArr);
//    // 遍历
//    for (Movie *tempMovie in movieArr) {
//        if ([tempMovie.movieId isEqualToString:movie.movieId]) {
//            NSLog(@"1");
//            return NO;
//        }
//    }
//    NSLog(@"2");
//    return YES;
//}
//
//+ (void)saveMovieToPlist:(Movie *)movie {
//    NSString *sandBoxPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
//    NSString *docPath = [sandBoxPath stringByAppendingPathComponent:@"movie/movie.plist"];
//    // 直接归档会覆盖掉之前的数据,所以先进行反归档,然后填加进去之后再进行归档操作
//    NSMutableArray *movieArr = [NSKeyedUnarchiver unarchiveObjectWithFile:docPath];
//    if (movieArr == nil) {
//        // 如果为空,先进行初始化
//        movieArr = [NSMutableArray array];
//    }
//    [movieArr addObject:movie];
//    // 归档
//    [NSKeyedArchiver archiveRootObject:movieArr toFile:docPath];
//    NSLog(@"%@", docPath);
//}
//
//+ (void)cancleMovieInPlist:(Movie *)movie {
//    NSString *sandBoxPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
//    NSString *docPath = [sandBoxPath stringByAppendingPathComponent:@"movie/movie.plist"];
//    // 反归档
//    NSMutableArray *movieArr = [NSKeyedUnarchiver unarchiveObjectWithFile:docPath];
//    // 遍历之后移除相应的对象
//    for (Movie *tempMovie in movieArr) {
//        if ([tempMovie.movieId isEqualToString:movie.movieId]) {
//            [movieArr removeObject:tempMovie];
//            break;
//        }
//    }
//    [NSKeyedArchiver archiveRootObject:movieArr toFile:docPath];
//}


+ (void)saveFileToPlist:(id)object type:(SaveToolType)type {
    
    switch (type) {
        case SaveToolCartoonBookCase: {
            ZBCartoonHistoryModel *cartoonHistory = object;
            ZBHomeCartoonModel *cartoon = cartoonHistory.cartoon;
            
            NSString *sandBoxPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
//            NSString *idPath = [NSString stringWithFormat:@"BookCase/%@/%@.plist", cartoon.albumId, cartoon.albumId];
            NSString *docPath = [sandBoxPath stringByAppendingPathComponent:@"BookCase/bookCase.plist"];
            // 直接归档会覆盖掉之前的数据,所以先进行反归档,然后填加进去之后再进行归档操作
//            NSFileManager *manager = [NSFileManager defaultManager];
//            if (![manager fileExistsAtPath:docPath]) {
//                [manager createDirectoryAtPath:docPath withIntermediateDirectories:YES attributes:nil error:nil];
//            }
            NSMutableArray *bookCaseArr = [NSKeyedUnarchiver unarchiveObjectWithFile:docPath];
            if (bookCaseArr == nil) {
                // 如果为空,先进行初始化
                bookCaseArr = [NSMutableArray array];
            }
            
//            for (ZBCartoonHistoryModel *tempch in bookCaseArr) {
//                ZBHomeCartoonModel *tempCartoon = tempch.cartoon;
//                if ([cartoon.albumId isEqualToNumber:tempCartoon.albumId]) {
//                    tempch = cartoonHistory;
//                    [NSKeyedArchiver archiveRootObject:bookCaseArr toFile:docPath];
//                    break;
//                }
//            }
            NSInteger count = 1;
            for (NSInteger i = 0; i < bookCaseArr.count; i++) {
                ZBHomeCartoonModel *tempCartoon = [bookCaseArr[i] cartoon];
                if ([cartoon.albumId isEqualToNumber:tempCartoon.albumId]) {
                    [bookCaseArr replaceObjectAtIndex:i withObject:cartoonHistory];
                    [NSKeyedArchiver archiveRootObject:bookCaseArr toFile:docPath];
                    count ++;
                }
            }
            
            if (count == 1) {
                [bookCaseArr addObject:cartoonHistory];
                // 归档
                [NSKeyedArchiver archiveRootObject:bookCaseArr toFile:docPath];
                break;
            }
            break;
        }
            
        case SaveToolCartoonHistory: {
            ZBCartoonHistoryModel *cartoonHistory = object;
            ZBHomeCartoonModel *cartoon = cartoonHistory.cartoon;
            
            NSString *sandBoxPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
            NSString *docPath = [sandBoxPath stringByAppendingPathComponent:@"CartoonHistory/history.plist"];
            // 直接归档会覆盖掉之前的数据,所以先进行反归档,然后填加进去之后再进行归档操作
            NSMutableArray *historyArr = [NSKeyedUnarchiver unarchiveObjectWithFile:docPath];
            if (historyArr == nil) {
                // 如果为空,先进行初始化
                historyArr = [NSMutableArray array];
            }
            
            NSArray *arr = [NSArray arrayWithArray:historyArr];
            
            for (ZBCartoonHistoryModel *tempch in arr) {
                ZBHomeCartoonModel *tempCartoon = tempch.cartoon;
                if ([cartoon.albumId isEqualToNumber:tempCartoon.albumId]) {
                    [historyArr removeObject:tempch];
                }
            }
            
            [historyArr addObject:cartoonHistory];
            // 归档
            [NSKeyedArchiver archiveRootObject:historyArr toFile:docPath];
            break;
        }
            
        default:
            break;
    }
}

#if 1

+ (void)cancleFileInPlist:(id)object type:(SaveToolType)type {
    
    switch (type) {
        case SaveToolCartoonBookCase: {
            
            NSString *sandBoxPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
            NSString *docPath = [sandBoxPath stringByAppendingPathComponent:@"BookCase/bookCase.plist"];
            // 反归档
            NSMutableArray *bookCaseArr = [NSKeyedUnarchiver unarchiveObjectWithFile:docPath];
            
            ZBCartoonHistoryModel *cartoonHistory = object;
            ZBHomeCartoonModel *cartoon = cartoonHistory.cartoon;
            
            // 遍历之后移除相应的对象
            for (ZBCartoonHistoryModel *tempch in bookCaseArr) {
                ZBHomeCartoonModel *tempCartoon = tempch.cartoon;
                if ([cartoon.albumId isEqualToNumber:tempCartoon.albumId]) {
                    [bookCaseArr removeObject:tempch];
                    break;
                }
            }
            [NSKeyedArchiver archiveRootObject:bookCaseArr toFile:docPath];
            break;
        }
            
        case SaveToolCartoonHistory: {
            NSString *sandBoxPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
            NSString *docPath = [sandBoxPath stringByAppendingPathComponent:@"CartoonHistory/history.plist"];
            // 反归档
            NSMutableArray *historyArr = [NSKeyedUnarchiver unarchiveObjectWithFile:docPath];
            
            ZBCartoonHistoryModel *cartoonHistory = object;
            ZBHomeCartoonModel *cartoon = cartoonHistory.cartoon;
            
            // 遍历之后移除相应的对象
            for (ZBCartoonHistoryModel *tempch in historyArr) {
                ZBHomeCartoonModel *tempCartoon = tempch.cartoon;
                if ([cartoon.albumId isEqualToNumber:tempCartoon.albumId]) {
                    [historyArr removeObject:tempch];
                    break;
                }
            }
            [NSKeyedArchiver archiveRootObject:historyArr toFile:docPath];
            break;
        }
            
            
        default:
            break;
    }
}

+ (BOOL)isHaveInFile:(id)object type:(SaveToolType)type {
    
    switch (type) {
        case SaveToolCartoonBookCase: {
            // 拼接电影的文件夹路径
            NSString *sandBoxPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
            NSString *docPath = [sandBoxPath stringByAppendingPathComponent:@"BookCase/bookCase.plist"];
            NSMutableArray *bookCaseArr = [NSKeyedUnarchiver unarchiveObjectWithFile:docPath];
            
//            ZBCartoonHistoryModel *cartoonHistory = object;
//            ZBHomeCartoonModel *cartoon = cartoonHistory.cartoon;
            
            ZBHomeCartoonModel *cartoon = object;
            
            for (ZBCartoonHistoryModel *tempch in bookCaseArr) {
                ZBHomeCartoonModel *tempCartoon = tempch.cartoon;
                if ([cartoon.albumId isEqualToNumber:tempCartoon.albumId]) {
                    return YES;
                    break;
                }
            }
            break;
        }
        
        case SaveToolCartoonHistory: {
            // 拼接电影的文件夹路径
            NSString *sandBoxPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
            NSString *docPath = [sandBoxPath stringByAppendingPathComponent:@"CartoonHistory/history.plist"];
            NSMutableArray *historyArr = [NSKeyedUnarchiver unarchiveObjectWithFile:docPath];
            
//            ZBCartoonHistoryModel *cartoonHistory = object;
//            ZBHomeCartoonModel *cartoon = cartoonHistory.cartoon;
            
            NSNumber *albumId = object;
            
            for (ZBCartoonHistoryModel *tempch in historyArr) {
                ZBHomeCartoonModel *tempCartoon = tempch.cartoon;
                if ([albumId isEqualToNumber:tempCartoon.albumId]) {
                    return YES;
                    break;
                }
            }
            break;
        }
            
            
        default:
            break;
    }
    return NO;
}

+ (id)getFile:(id)object type:(SaveToolType)type {
    switch (type) {
        case SaveToolCartoonBookCase: {
            // 拼接电影的文件夹路径
            NSString *sandBoxPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
            NSString *docPath = [sandBoxPath stringByAppendingPathComponent:@"BookCase/bookCase.plist"];
            NSMutableArray *bookCaseArr = [NSKeyedUnarchiver unarchiveObjectWithFile:docPath];
            
            //            ZBCartoonHistoryModel *cartoonHistory = object;
            //            ZBHomeCartoonModel *cartoon = cartoonHistory.cartoon;
            
            ZBHomeCartoonModel *cartoon = object;
            
            for (ZBCartoonHistoryModel *tempch in bookCaseArr) {
                ZBHomeCartoonModel *tempCartoon = tempch.cartoon;
                if ([cartoon.albumId isEqualToNumber:tempCartoon.albumId]) {
                    return tempch;
                    break;
                }
            }
            break;
        }
            
        case SaveToolCartoonHistory: {
            // 拼接电影的文件夹路径
            NSString *sandBoxPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
            NSString *docPath = [sandBoxPath stringByAppendingPathComponent:@"CartoonHistory/history.plist"];
            NSMutableArray *historyArr = [NSKeyedUnarchiver unarchiveObjectWithFile:docPath];
            
            //            ZBCartoonHistoryModel *cartoonHistory = object;
            //            ZBHomeCartoonModel *cartoon = cartoonHistory.cartoon;
            
            NSNumber *albumId = object;
            
            for (ZBCartoonHistoryModel *tempch in historyArr) {
                ZBHomeCartoonModel *tempCartoon = tempch.cartoon;
                if ([albumId isEqualToNumber:tempCartoon.albumId]) {
                    return tempch;
                }
            }
            break;
        }
            
            
        default:
            break;
    }
    return nil;
}

#endif
@end
#endif
